#include "pin_config.h"
#include "var.h"
#include "fun_tools.h"


void in_systick_int(void) {
    if (delay5ms != 0x00) {
        delay5ms--;
    }
    if (delay5ms == 0x00) {
        delay5ms = 5;
        delay_5ms_flg = 1;
    }
}

/**
 * 处理pc数据
 */
void PCSet_Data_Deal(void) {
    if (!PCSet_overtime_flg) {
        return;
    }
    PCSet_overtime_flg = 0;

#if PCSET_UART_DEBUG
    printf_buf_hex(PCSetBuff, PCSetBuffCnt);
    printf("\r\n pcsetBuff=%d \r\n", PCSetBuff[0]);
    printf("PCSetBuff_Data_Deal [len: %u]\r\n", PCSetBuffCnt);
#endif
    //以下可以将数据读取进行放到flash,或者做其它处理
//    if ((PCSetBuff[0] == 'A') && (PCSetBuff[1] == 'W')) {
//        switch (PCSetBuff[2]) {
//            case 0x01:  // 写设备编号(随后4个字节设备编号)
//            {
//
//            }
//            case 0x02:  // 读设备编号
//            {
//
//            }
//        }
//    }
    PCSetBuffCnt = 0;
}


void delay_5ms_deal(void) {
    if (!delay_5ms_flg) {
        return;
    }
    delay_5ms_flg = 0;

    if (delay1Sec != 0x00) {
        delay1Sec--;
    }
    if (delay1Sec == 0x00) {
        delay1Sec = 200;
        delay_1s_flg = 1;
    }

    if (PCSet_start_flg) {
        if (PCSetDelay != 0x00) {
            PCSetDelay--;
        }
        if (PCSetDelay == 0x00) {
            PCSet_start_flg = 0;
            PCSet_overtime_flg = 1;
        }
    }
}

void delay_1s_deal(void) {
    if (!delay_1s_flg) {
        return;
    }
    delay_1s_flg = 0;
}

/**
 * 初始化系统
 */
void Init_System(void) {
    Debug_UART_Configuration();
    SysTick_Init();

}

int main() {
    FL_Init();
    SystemClockSel_RCHF(FCLK);
    Init_System();
    while (1) {
        delay_5ms_deal();
        delay_1s_deal();
        PCSet_Data_Deal();
    }
}